/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 d3leaf@126.com
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package com.mapperdb.provider;

import static com.mapperdb.helper.SqlNodeHelper.getIfColumnNodeAll;
import static com.mapperdb.helper.SqlNodeHelper.getPkColumnNode;

import java.util.Map;
import org.apache.ibatis.scripting.xmltags.SqlNode;
import org.apache.ibatis.scripting.xmltags.StaticTextSqlNode;

public class SelectMapperProvider extends BaseMapperProvider {

	/**
	 * 查询
	 * @return
	 */
	public SqlNode selectOne() {
		return select();
	}

	/**
	 * 查询
	 * @return
	 */
	public SqlNode select() {
		//静态的sql部分:select column ... from table
		addSqlNode(selectColumns());
		//将if添加到<where>
		addWhereSqlNode(getIfColumnNodeAll(resultEntity));
		return toSqlNode();
	}

	/**
	 * 查询
	 * @return
	 */
	public SqlNode selectList() {
		return select();
	}

	/**
	 * 查询
	 * @return
	 */
	public SqlNode selectByPrimaryKey() {
		//静态的sql部分:select column ... from table
		addSqlNode(selectColumns());
		//where/if判断条件
		addSqlNode(pkSqlNode());
		return toSqlNode();
	}

	/**
	 * 查询
	 * @return
	 */
	public SqlNode selectByPrimaryKeys() {
		//静态的sql部分:select column ... from table
		addSqlNode(selectColumns());
		//where/if判断条件
		addSqlNode(pksSqlNode());
		return toSqlNode();
	}

	/**
	 * 查询
	 * @return
	 */
	public SqlNode selectByPrimaryKeySelective() {
		//静态的sql部分:select column ... from table
		addSqlNode(selectColumns());
		//where/if判断条件
		addWhereSqlNode(getPkColumnNode(resultEntity));
		return toSqlNode();
	}

	/**
	 * 查询总数
	 * @return
	 */
	public SqlNode selectCount() {
		//select count(*) from table
		addSqlNode("SELECT COUNT(1) FROM " + resultEntity.tableName());
		//获取全部列的where,if条件
		addWhereSqlNode(getIfColumnNodeAll(resultEntity));
		return toSqlNode();
	}

	/**
	 * 查询总数
	 * @return
	 */
	public SqlNode selectCountByWhereSql(Map<String, Object> paramMap) {
		//select count(*) from table
		addSqlNode("SELECT COUNT(1) FROM " + resultEntity.tableName());
		//获取全部列的where,if条件
		addWhereSqlNode(whereSql(paramMap));
		return toSqlNode();
	}

	/**
	 * 查询
	 * @return
	 */
	public SqlNode selectListByWhereSql(Map<String, Object> paramMap) {
		return selectOneByWhereSql(paramMap);
	}

	/**
	 * 查询
	 * @return
	 */
	public SqlNode selectOneByWhereSql(Map<String, Object> paramMap) {
		//静态的sql部分:select column ... from table
		addSqlNode(selectColumns());
		//将if添加到<where>
		addWhereSqlNode(whereSql(paramMap));
		return toSqlNode();
	}

	//静态的sql部分:select column ... from table
	SqlNode selectColumns() {
		return new StaticTextSqlNode("SELECT " + resultEntity.getAsSelectColumns() + " FROM " + resultEntity.tableName());
	}
}
